package cn.tedu.project.oa.note.controller;

import cn.tedu.project.oa.base.response.JsonResult;
import cn.tedu.project.oa.base.response.PageData;
import cn.tedu.project.oa.base.webSocket.CustomWebSocketEndpoint;
import cn.tedu.project.oa.note.pojo.dto.NoteQuery;
import cn.tedu.project.oa.note.pojo.dto.NoteSaveParam;
import cn.tedu.project.oa.note.pojo.vo.NoteVO;
import cn.tedu.project.oa.note.service.NoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import shade.com.alibaba.fastjson2.JSON;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/oa/note")
@Api(tags = "05.首页模板-备忘录")
@Slf4j
public class NoteController {

    @Autowired
    NoteService noteService;

    @ApiOperation("查询备忘录列表")
    @GetMapping("select")
    public JsonResult selectList(NoteQuery noteQuery){
      log.debug("查询备忘录列表:noteQuery={}",noteQuery);

        PageData data = noteService.selectList(noteQuery);

//      List<NoteVO> list = noteService.selectList(noteQuery);

      return JsonResult.ok(data);

    }

    @ApiOperation("显示备忘详情")
    @GetMapping("show")
    public JsonResult showNote(Long id){
        log.debug("显示备忘详情:id={}",id);

        NoteVO noteVO = noteService.selectById(id);

        return JsonResult.ok(noteVO);
    }

    @ApiOperation("发布备忘")
    @PostMapping("save")
    public JsonResult saveNote(NoteSaveParam noteSaveParam){
        log.debug("发布备忘:noteSaveParam={}",noteSaveParam);

        noteService.saveNote(noteSaveParam);

        return JsonResult.ok();
    }

    @ApiOperation("删除备忘")
    @PostMapping("delete/{id}/{userId}")
    public JsonResult deleteNote (@PathVariable Long id, @PathVariable Long userId){
        log.debug("删除备忘录:id={},userId={}",id,userId);

        noteService.deleteById(id);

        return JsonResult.ok();

    }

    //TODO被Token拦截站内信测试
    @GetMapping("addNum")
    @ResponseBody
    public String sendMessage() {
        String userId = "6";
        Map<String,String> map = new HashMap<>();
        map.put("message","新消息");
        String message = "{message : '新消息'}";

        try {
            CustomWebSocketEndpoint.sendMessageToUser(userId, JSON.toJSONString(map));
            return "发送消息成功:" + userId;
        } catch (IOException e) {
            e.printStackTrace();
            return "发送消息失败";
        }
    }
}
